package tech.eyao.hs60x.serviceImpl;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tech.eyao.hs60x.common.CommonConstants;
import tech.eyao.hs60x.common.ConstantsKey;
import tech.eyao.hs60x.converts.StepConvert;
import tech.eyao.hs60x.dao.AccountDao;
import tech.eyao.hs60x.dao.AccountRelationDao;
import tech.eyao.hs60x.dao.StepDao;
import tech.eyao.hs60x.dto.BaseResult;
import tech.eyao.hs60x.dto.StepDTO;
import tech.eyao.hs60x.enums.AccountType;
import tech.eyao.hs60x.model.domain.AccountDO;
import tech.eyao.hs60x.model.domain.AccountRelationDO;
import tech.eyao.hs60x.model.domain.StepDO;
import tech.eyao.hs60x.service.HealthService;
import tech.eyao.hs60x.utils.DateUtil;
import tech.eyao.hs60x.vo.StepQuery;

/**
 * 计步实现类
 * @author hejian
 * @date 2017年12月21日 下午5:07:18
 */
@Service
public class HealthServiceImpl implements HealthService {
	
	private static final Logger logger = LoggerFactory.getLogger(HealthServiceImpl.class);
	@Autowired
	private StepDao stepDao;
	@Autowired
	private AccountDao accountDao;
	@Autowired
	private AccountRelationDao accountRelationDao;
	@Autowired
	private StringRedisTemplate template;

	@Override
	public BaseResult uploadStepNumber(long watchId, int setpNumber) {
		try {
			AccountDO accountDO = accountDao.getAccountById(watchId);
			
			if(null == accountDO || AccountType.PHONE.getValue() == accountDO.getAccountType()) {
				logger.warn("[HealthService uploadStepNumber] watchId" + watchId + " setpNumber " + setpNumber);
				return BaseResult.isBussinessFail("账户 不存在");
			}
			
			StepDO stepDO = stepDao.getCurrDayStep(watchId,DateUtil.getFormatDate());
			if(null == stepDO) {
				stepDO = new StepDO();
				stepDO.setNickName(accountDO.getNickName());
				stepDO.setSetpDate(new Date());
				stepDO.setStep((long) setpNumber);
				stepDO.setThumbUp(0L);
				stepDO.setWatchId(watchId);
				stepDao.insert(stepDO);
			} else {
				stepDO.setStep((long) setpNumber);
				stepDao.updateByPrimaryKey(stepDO);
			}
		} catch (Exception e) {
			logger.error("[HealthService uploadStepNumber error.]", e);
		}
		return BaseResult.isSuccess();
	}

	@Override
	public BaseResult getUserStep(long wacthId) {
		try {
			StepQuery pageQuery = new StepQuery();
			pageQuery.setWatchId(wacthId);
			pageQuery.setStartDate(DateUtil.addDay(CommonConstants.SETP_QUERY_DAY));
			pageQuery.setEndDate(DateUtil.getFormatDate());
			List<StepDO> lists = stepDao.list(pageQuery);
			return BaseResult.isSuccess(StepConvert.convert(lists));
		} catch (Exception e) {
			logger.error("[HealthService getUserSetp error.]", e);
		}
		return BaseResult.isSuccess();
	}

	@Override
	public BaseResult topList(long watchId) {
		List<AccountRelationDO> accountRelationDOs = accountRelationDao.getWatchRelationList(watchId);
		if(CollectionUtils.isEmpty(accountRelationDOs)) {
			return BaseResult.isDeFaultNoData();
		}
		
		List<StepDTO> list = accountRelationDOs.stream().map(AccountRelationDO::getRelationId).collect(Collectors.toList()).stream().map((i)->{
			return StepConvert.convertDTO(template.hasKey(ConstantsKey.getStepFavourKey(watchId,i)),stepDao.getCurrDayStep(i, DateUtil.getFormatDate()));
		}).collect(Collectors.toList());
		
		return BaseResult.isSuccess(list);
	}

	@Override
	public BaseResult favour(long watchId, long friendWatchId) {
		
		if(watchId == friendWatchId) {
			return BaseResult.isBussinessFail("不能给自己点赞");
		}
		
		if(template.hasKey(ConstantsKey.getStepFavourKey(watchId,friendWatchId))) {
			return BaseResult.isSuccess();
		}
		template.opsForValue().set(ConstantsKey.getStepFavourKey(watchId,friendWatchId), "1", DateUtil.getTodayRemainingSeconds(), TimeUnit.SECONDS);
		return BaseResult.isSuccess();
	}
	
	
	
}
